package com.novellotus.iov.carvice.activity;

import android.Manifest;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jakewharton.rxbinding2.view.RxView;
import com.novellotus.iov.carvice.R;
import com.novellotus.iov.carvice.adapter.WorkFragmentAdapter;
import com.novellotus.iov.carvice.bean.RxEinfos;
import com.novellotus.iov.carvice.bean.RxLoginBean;
import com.novellotus.iov.carvice.retrofit.BaseArrayEntity;
import com.novellotus.iov.carvice.retrofit.BaseArrayObserver;
import com.novellotus.iov.carvice.retrofit.BaseEntity;
import com.novellotus.iov.carvice.retrofit.BaseObserver;
import com.novellotus.iov.carvice.retrofit.RetrofitFactory;
import com.novellotus.iov.carvice.retrofit.RxSchedulers;
import com.novellotus.iov.carvice.utils.DataSign;
import com.novellotus.iov.carvice.utils.MyUtils;
import com.novellotus.iov.carvice.utils.SharedPreferencesUtils;

import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * 反馈页面
 */
public class FeedBackActivity extends BaseActivity {

    @BindView(R.id.tv_tittle)
    TextView tv_tittle;
    @BindView(R.id.edt_submit)
    EditText edt_submit;
    @BindView(R.id.btn_submit)
    Button btn_submit;


    @Override
    protected void initContentView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_feed_back);
    }

    @Override
    public void init() {
        tv_tittle.setText("意见反馈");
        initSubmit();

    }

    @OnClick(R.id.img_back)
    void back() {
        finish();
    }

    /**
     * 获取角色列表
     */
    public void initSubmit() {
        RxView.clicks(btn_submit).compose(RxSchedulers.lifecycle(this)).throttleFirst(2, TimeUnit.SECONDS).compose(rxPermissions.ensureEach(Manifest.permission.WRITE_EXTERNAL_STORAGE)).subscribe(new Consumer<Object>() {
            @Override
            public void accept(Object o) throws Exception {

                if (edt_submit.getText().toString().length() > 0) {
                    RequestBody body = RequestBody.create(okhttp3.MediaType.parse("text/plain; charset=utf-8"), edt_submit.getText().toString());
                    Observable<BaseEntity<String>> observable = RetrofitFactory.getInstance().feedback(SharedPreferencesUtils.getKey(FeedBackActivity.this)[0], body);
                    observable.compose(RxSchedulers.<BaseEntity<String>>compose())
                            .subscribe(new BaseObserver<String>(FeedBackActivity.this) {
                                @Override
                                public void onSuccess(String s) {
                                    MyUtils.showToast(FeedBackActivity.this, "反馈提交成功!");
                                    finish();
                                }

                                @Override
                                public void onFail(int code, String msg) {

                                }

                                @Override
                                public void onError(String msg) {

                                }
                            });
                } else {
                    MyUtils.showToast(FeedBackActivity.this, "提交内容不能为空");
                }

            }

        });

    }

}
